import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Date;

public class T {
    public static void main(String[] args)throws IOException {
        Long sys = System.currentTimeMillis();
        System.out.println(sys);
       /* FileInputStream fileInputStream = new FileInputStream("C:\\\\Users\\\\wwz\\\\Desktop\\\\sql.txt");
        byte[] bytes = new byte[1024];
        int len = 0;
        while( (len = fileInputStream.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len,"UTF-8"));
        }*/
        //获取文件
        RandomAccessFile rfile = new RandomAccessFile("C:\\Users\\wwz\\Desktop\\sql.txt", "rw");
        //获取文件通道
        FileChannel channel =  rfile.getChannel();
        //设置缓冲区
        ByteBuffer buff = ByteBuffer.allocate(1024);
        //读取缓冲区的数据
        int data = channel.read(buff);
        while(data != -1 ){
            System.out.println("========================");
            //通过flip()方法将Buffer从写模式切换到读模式。在读模式下，可以读取之前写入到buffer的所有数据。
            buff.flip();

            while(buff.hasRemaining()){
                System.out.print((char)buff.get());
            }
           /* *
             * 有两种方式能清空缓冲区：调用clear()或compact()方法。
             * clear()方法会清空整个缓冲区。compact()方法只会清除已经读过的数据。
             * 任何未读的数据都被移到缓冲区的起始处，新写入的数据将放到缓冲区未读数据的后面。*/

            buff.clear();
            data = channel.read(buff);
        }
        rfile.close();
        System.out.println(System.currentTimeMillis()-sys);
    }
}
